<?php
namespace app\admin\model;

use think\Model;
use think\Db;
use think\facade\Config;
class Classify extends Model
{   

	/*查询数据 */
	public function get_data($page,$limit,$sear){
		$start=($page-1)*$limit;
        if(empty($sear)){
          $list=Classify::order('id', 'asc')->where([['status','=',1],['is_tag','=',0]])->limit($start,$limit)->select()->toArray();
          $data['count']=Classify::where([['status','=',1],['is_tag','=',0]])->count();
        }else{
          $list=Classify::where([['cname','like','%'.$sear.'%']])->order('id', 'asc')->where([['status','=',1],['is_tag','=',0]])->limit($start,$limit)->select()->toArray();
         
          $data['count']=Classify::where([['cname','like','%'.$sear.'%'],['status','=',1],['is_tag','=',0]])->count(); 
        }
        
        $data['list']=$list;
        return $data;
	}

    /*获取联动数据*/
    public function get_sel($fid){
        $list=Classify::where([['status','=',1],['fid','=',$fid]])->field('id,cname,fid')->order('id','asc')->select()->toArray();
        return $list;
    }

    /*查询一条数据*/
    public function get_one($id){
        $list=Classify::where([['id','=',$id]])->where([['status','=',1],['is_tag','=',0]])->find();
        return $list;
    }  

	/*新增*/
	public function insert_data($cname,$fid,$level,$img){
        $Blog = new Classify;
		$result=$Blog->save([
		    'cname'  =>  trim($cname),
            'fid' =>  trim($fid),
            'level' =>  trim($level),
		    'img' =>  trim($img),
		    'create_time' => date("Y-m-d H:i:s",time())
		]);
		return $result;
	}
	/*修改*/
	public function update_data($id,$cname,$fid,$level,$img){
        $data = Classify::get($id);
        $data->cname = $cname;
        $data->fid = $fid;
        $data->level = $level;
        $data->img = $img;
        $result=$data->save();
		return $result;    
	}
	/*删除假动作实际为修改操作*/
	public function delete_data($id){
        $one=Classify::where([['status','=',1],['id','=',$id]])->find();
        if($one['level']==4){
            $data = Classify::get($id);
            $data->status = '2';
            $result=$data->save();
        }
        if($one['level']==3){
            $data = Classify::get($id);
            $data->status = '2';
            $result=$data->save();
            $two=Classify::where([['status','=',1],['fid','=',$id]])->select();
            if(!empty($two)){
              foreach ($two as $key => $value) {
                $data = Classify::get($value['id']);
                $data->status = '2';
                $result=$data->save();
              }
            }
        }
        if($one['level']==2){
            $data = Classify::get($id);
            $data->status = '2';
            $result=$data->save();
            $two=Classify::where([['status','=',1],['fid','=',$id]])->select();
            if(!empty($two)){
              foreach ($two as $key => $value) {
                $data = Classify::get($value['id']);
                $data->status = '2';
                $result=$data->save();
                $three=Classify::where([['status','=',1],['fid','=',$value['id']]])->select();
                if(!empty($three)){
                    foreach ($three as $k => $v) {
                      $data = Classify::get($v['id']);
                      $data->status = '2';
                      $result=$data->save();
                    }
                }
              }
            }
        }
        if($one['level']==1){
            $data = Classify::get($id);
            $data->status = '2';
            $result=$data->save();
            $two=Classify::where([['status','=',1],['fid','=',$id]])->select();
            if(!empty($two)){
              foreach ($two as $key => $value) {
                $data = Classify::get($value['id']);
                $data->status = '2';
                $result=$data->save();
                $three=Classify::where([['status','=',1],['fid','=',$value['id']]])->select();
                if(!empty($three)){
                    foreach ($three as $k => $v) {
                      $data = Classify::get($v['id']);
                      $data->status = '2';
                      $result=$data->save();
                      $four=Classify::where([['status','=',1],['fid','=',$v['id']]])->select();
                      if(!empty($four)){
                        foreach ($four as $k1 => $v1) {
                          $data = Classify::get($v1['id']);
                          $data->status = '2';
                          $result=$data->save();
                        }
                      }
                    }
                }
              }
            }
        }
		return $result;
	}

    /*逆向获取级联信息 用于第三级或者第四级逆向查询 $id为分类id*/
	public function get_cascade_data($id){
		/*第一步先根据获取当前id的信息 根据level的值来确定 向上查询几级*/
        $class[0]['xz']=Classify::where([['status','=',1],['id','=',$id]])->field('cname,fid,id,level')->find();
        $class[0]['all']=Classify::where([['status','=',1],['fid','=',$class[0]['xz']['fid']]])->field('cname,fid,id,level')->select();
        
        $search_num=$class[0]['xz']['level']-1;/*向上查询次数*/
        for($i=1;$i<=$search_num;$i++){
        	$j=$i-1;
        	$class[$i]['xz']=Classify::where([['status','=',1],['id','=',$class[$j]['xz']['fid']]])->field('cname,fid,id,level')->find();
            $class[$i]['all']=Classify::where([['status','=',1],['fid','=',$class[$i]['xz']['fid']]])->field('cname,fid,id,level')->select();
        }
        return $class;

	}
    


    public function update_zhedit($id,$zh_des,$zh_des_s,$zh_h,$zh_title,$zh_keyword,$zh_description,$sel_zh_title,$sel_zh_keyword,$sel_zh_description,$zh_d_title,$zh_d_keyword,$zh_d_description,$zh_content){
        
        $data = Classify::get($id);
        $data->zh_des = trim($zh_des);
        $data->zh_des_s = trim($zh_des_s);
        $data->zh_h = trim($zh_h);
        $data->zh_title = trim($zh_title);
        $data->zh_keyword = trim($zh_keyword);
        $data->zh_description = trim($zh_description);
        $data->sel_zh_title = trim($sel_zh_title);
        $data->sel_zh_keyword = trim($sel_zh_keyword);
        $data->sel_zh_description = trim($sel_zh_description);
        $data->zh_d_title = trim($zh_d_title);
        $data->zh_d_keyword = trim($zh_d_keyword);
        $data->zh_d_description = trim($zh_d_description);
        $data->zh_content = trim($zh_content);

        
        $result=$data->save();
        return $result;
    }

    public function update_enedit($id,$des,$en_des_s,$en_h,$title,$keyword,$description,$sel_title,$sel_keyword,$sel_description,$d_title,$d_keyword,$d_description,$content,$is_pay){
        
        $data = Classify::get($id);
        $data->des = trim($des);
        $data->en_des_s = trim($en_des_s);
        $data->en_h = trim($en_h);
        $data->title = trim($title);
        $data->keyword = trim($keyword);
        $data->description = trim($description);
        $data->sel_title = trim($sel_title);
        $data->sel_keyword = trim($sel_keyword);
        $data->sel_description = trim($sel_description);
        $data->d_title = trim($d_title);
        $data->d_keyword = trim($d_keyword);
        $data->d_description = trim($d_description);
        $data->content = trim($content);
        $data->is_pay = $is_pay;

        
        $result=$data->save();
        return $result;
    }
}
 